package com.wp.controller.impl;

import com.wp.entity.Supplier;
import com.wp.service.SupplierService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/sys/supplier")
public class SupplierController {

    // 注入属性
    @Resource
    private SupplierService supplierService;

    @GetMapping("/list")
    public String findAll(Model model) {
        List<Supplier> suppliers = supplierService.findAll();

        model.addAttribute("supplierList", suppliers); // 存储信息

        return "supplier/list";
    }


    // 模糊查询
    @PostMapping("/findSupply")
    public String findSupply(Model model, @RequestParam(required = false)
            String supCode, @RequestParam(required = false) String supName) {


        List<Supplier> suppliers = supplierService.findSupply(supCode, supName);

        model.addAttribute("supplierList", suppliers); // 存储信息

        if (suppliers != null) {
            return "supplier/list";
        } else {
            return null;
        }
    }

    @RequestMapping("/toAdd")
    public String toAdd() {

        return "supplier/add";
    }

    // 添加信息
    @PostMapping("/add")
    public String add(Supplier supplier) {

        int add = supplierService.toAdd(supplier);

        if (add>0){
            return "redirect:/sys/supplier/list";
        }else {
            return null;
        }
    }
    // 删除信息
    @DeleteMapping("/del/{supId}")
    @ResponseBody // 页面不跳转》》用于异步请求
    public String del(@PathVariable (name = "supId") Integer id) {

        int del = supplierService.del(id);

        if (del>0){
            return "{\"delResult\":\"true\"}";
        }else {
            return "{\"delResult\":\"false\"}";
        }
    }

    // 按编号查询
    @GetMapping("/view/{supId}")
    public String findById(Model model,@PathVariable(name = "supId") Integer id) {

        Supplier supplier = supplierService.findById(id);

        model.addAttribute("supplier",supplier);

        return "supplier/view";
    }

    // 查询个人信息
    @RequestMapping("/toUpdate/{supId}")
    public String toUpdate(Model model,@PathVariable(name = "supId")Integer id) {

        Supplier supplier = supplierService.findById(id);

        model.addAttribute("supplier",supplier);

        return "supplier/upd";
    }


    // 修改信息
    @PostMapping("/upd")
    public String upd(Supplier supplier) {

        int upd = supplierService.upd(supplier);

        if (upd>0){
            return "redirect:/sys/supplier/list";
        }else {
            return null;
        }
    }
}
